﻿using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using WxLib.Core.Util;
using WxLib.Core.Serializers;
using WxLib.Core.RequestHandlers;
using WxLib.Common;
using WxLib.Business;
using WxLib.QRCode;
using wxLib.QRCode.Models;
using System.Threading;
using System.Text;
using wxLib.QRCode.BaiDu;

namespace wxLib.QRCode
{
    public class BaiDuQRCode
    {

        /// <summary>
        /// 获取或设置用于执行请求的RequestHandler
        /// </summary>
        public IPostRequestHandler RequestHandler;

        public IRequestHandler ReHandler;


        /// <summary>
        /// 构造函数
        /// </summary>
        public BaiDuQRCode()
        {
            RequestHandler = new RequestHandler(new JsonSerializer());
            ReHandler = new RequestHandler(new JsonSerializer());
        }

        /// <summary>
        /// code 换取 session_key
        /// </summary>
        /// <param name="loginCode"></param>
        /// <returns></returns>
        public async Task<BaiduCode2SessionResponse> Jscode2sessionkey(BaiDuAuthenticationRequest request)
        {
            string relUri = string.Format(@"https://spapi.baidu.com/oauth/jscode2sessionkey?code={0}&client_id={1}&sk={2}", request.code, request.client_id, request.sk);
            string jsonStr = "{\"code\": \"" + request.code + "\",\"client_id\": \"" + request.client_id + "\",\"sk\": \"" + request.sk + "\" }";
            return await RequestHandler.PostRequestAsync<BaiduCode2SessionResponse>(new Uri(relUri), jsonStr);
        }
    }
}
